package com.dyz.leetcode._07_hashSearch;

import java.util.HashSet;
import java.util.Set;

public class _888_fairCandySwap {
    public int[] fairCandySwap(int[] aliceSizes, int[] bobSizes) {
        //A - x + y = B - y + x
        //=> x = y + (A - B) / 2
        int A = 0;
        for(int a:aliceSizes){
            A+=a;
        }
        int B = 0;
        for(int b:bobSizes){
            B+=b;
        }
        int delta = (A-B)/2;

        Set<Integer> set = new HashSet<>();
        for(int ali: aliceSizes){
            set.add(ali);
        }
        int[] ans = new int[2];
        for(int y:bobSizes){
            int x = y +delta;
            if(set.contains(x)){
                ans[0]=x;
                ans[1]=y;
                break;
            }
        }
    return ans;
    }
}
